﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SuperQuiz.Model;
using System.Collections.Generic;
using System.Linq;

namespace SuperQuiz
{
    public class MyFactory
    {

        public static Question CreateQuestion(string text, int right, params string[] list)
        {
            Question q = new Question();
            q.Text = text;
            q.MyAlternatives = new List<Alternative>();
            int i = 1;
            foreach (string s in list)
            {
                q.MyAlternatives.Add(new Alternative(s, i == right ? true : false));
                i++;
            }
            //q.Answer = right;
            //q.UserAnswer = 0;
            return q;

        }

        internal static UserEvaluation GetUserEvaluationFrom(Test test)
        {
            UserEvaluation userEvaluation = new UserEvaluation();

            userEvaluation.Name = test.Name;
            userEvaluation.Questions = new List<Question>();

            int questionCount = test.Questions.Count;
            Random rnd = new Random();
            var sequence = Enumerable.Range(1, questionCount).OrderBy(n => rnd.Next());

            foreach (int i in sequence)
            {
                Question questionFromTest = test.Questions[i - 1];
                Question newQuestion = new Question()
                {
                    Text = questionFromTest.Text,
                };

                newQuestion.MyAlternatives = new List<Alternative>();
                int alternativeCount = questionFromTest.MyAlternatives.Count;
                var sequence2 = Enumerable.Range(1, alternativeCount);
                if (alternativeCount > 3)
                {
                    sequence2 = Enumerable.Range(1, alternativeCount).OrderBy(n => rnd.Next());
                }

                foreach (int ii in sequence2)
                {
                    newQuestion.MyAlternatives.Add(new Alternative(questionFromTest.MyAlternatives[ii - 1].Text, questionFromTest.MyAlternatives[ii - 1].Value));
                }

                userEvaluation.Questions.Add(newQuestion);
            }

            return userEvaluation;
        }


        public static Summary GetSummary(UserEvaluation userEvaluation)
        {
            Summary retSummary = new Summary();

            retSummary.topicName = userEvaluation.Name;

            foreach (Question q in userEvaluation.Questions)
            {
                Boolean test = true;
                foreach (Alternative a in q.MyAlternatives)
                {
                    if (a.UserAnswer != a.Value)
                    {
                        test = false;
                        break;
                    }
                }
                if (test)
                {
                    retSummary.Right++;
                }
                else
                {
                    retSummary.Wrong++;
                }

            }

            retSummary.Total = retSummary.Wrong + retSummary.Right;
            return retSummary;

        }
    }
}
